<template>
	<view class="">
		<image style="position: fixed;top:calc( ( 100% - 287rpx ) / 3 );width: 260rpx;height: 287rpx;margin-left: calc( ( 100% - 260rpx ) / 2 );" src="../../static/image/components/qidong.png">
		</image>
		<tanchu :show="show" @change="change" :custom="true" :mask-click="false">
			<view class="genxin_content">
				<view class="guanbi">
				</view>
				<!-- <view class="genxin_title">新版本更新</view> -->
				<view class="genxin_neirong" style="text-align: left;" v-for="(item,index) in content_content"
					:key="index">{{index+1}} 、{{item}}</view>
				<view class="lijitiyan" @click="gengxin">
					<text v-if="progress==''">立即更新</text>
					<text v-else>已更新{{progress}}%</text>
				</view>
			</view>
		</tanchu>
	</view>
</template>
<style>
</style>

<script>
	export default {
		data() {
			return {
				show: false, //弹框
				type: '', //弹框
				content_content: [],
				info: '',
				downloadurl: '',
				progress: ''
			}
		},
		onLoad() {
			// #ifdef H5
			this.tiaozhuan();
			// #endif

			// #ifdef APP-PLUS
			// this.tiaozhuan()
			this.gengxin_qingqiu();
			console.log('8888888888888888')
			// #endif

			// #ifdef MP-WEIXIN
			this.tiaozhuan()
			// #endif 

		},
		methods: {
			gengxin_qingqiu: function() {
				console.log('--------------------')
				var tt = this;
				var ios = "ios";
				var Android = "Android";
				let openUrl = plus.os.name === 'iOS' ? ios : Android;
				var type_type = 1;
				if (plus.os.name == 'iOS') {
					type_type = 2
				} else {
					type_type = 1
				}
				console.log(type_type)
				// get  包
				plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
					console.log(widgetInfo.appid);
					console.log(widgetInfo.name);
					console.log(widgetInfo.version);
					console.log('bbbsdfsdfs');
					uni.request({
						url:'https://api.hiclassmate.top/api/version/getNewBigVersion', //线上
						// url: 'http://htxtest.365vipbox.com/api/version/getNewBigVersion', //线下
						data: {
							type: type_type,
						},
						method: 'POST',
						success: (res) => {
							console.log(res)
							console.log(res.data.data)
							console.log(res.data.code)
							if (res.data.code == 1) {
								console.log('vvvv')
								console.log(widgetInfo.version)
								console.log(res.data.data.newversion)
								if (res.data.data.newversion != widgetInfo.version) {
									tt.content_content = res.data.data.content.split(',');
									tt.info = res.data.data.bigorsmall;
									uni.removeStorageSync('token')
									tt.togglePopup('center', 'tip');
									tt.downloadurl = res.data.data.downloadurl;
								} else {
									tt.tiaozhuan()
									console.log('没有更新')
								}
							}
						},
						fail(res) {
							console.log(res + 'cuowu')
						}
					});
				});
			},
			gengxin: function() {
				var tt = this;
				if (tt.info == 2) {
					//走更新
					tt.lijigengxin(tt.downloadurl);
				} else { //apk
					//是 ios
					void plus.runtime.openURL(tt.downloadurl);
				}
			},
			lijigengxin: function(downloadurl) {
				var tt = this;
				const downloadTask = uni.downloadFile({
					url: downloadurl,
					success: (downloadResult) => {
						if (downloadResult.statusCode == 200) {
							console.log(downloadResult)
							plus.runtime.install(downloadResult.tempFilePath, {
								force: true
							}, function() {
								console.log('install success...');
								uni.hideLoading();
								plus.runtime.restart();
							}, function(e) {
								console.error('install fail...');
								console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
								uni.hideLoading();
							});
						}
					}
				});

				downloadTask.onProgressUpdate((res) => {
					tt.progress = res.progress;
					// console.log('下载进度' + res.progress);
					// console.log('已经下载的数据长度' + res.totalBytesWritten);
					// console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
				});
			},
			// 小程序
			xiaochengxu: function() {

			},
			tiaozhuan: function() {
				var that = this;
				const value = uni.getStorageSync('launchFlag');
				if (value) {
					console.log('111')
					that.publics.switchTab("/pages/one/index")
					// if (uni.getStorageSync('token')) {
					// 	that.publics.switchTab(
					// 		"/pages/one/index")
					// } else {
					// 	uni.redirectTo({
					// 		url: "/view/public/login"
					// 	})
					// 	console.log('bbbb')
					// }
					// this.publics.switchTab('../index/index');
				} else {
					// 没有值，跳到引导页，并存储，下次打开就不会进去引导页
					uni.setStorage({
						key: 'launchFlag',
						data: true
					});
					console.log('22')
					this.publics.redirectTo('/view/public/yindaoye');
				}
			},
			togglePopup(type, open) {
				switch (type) {
					case 'top':
						this.content = '顶部弹出 popup'
						break
					case 'bottom':
						this.content = '底部弹出 popup'
						break
					case 'center':
						this.content = '居中弹出 popup'
						break
				}
				this.type = type
				if (open === 'tip') {
					this.show = true
				} else {
					this.$refs[open].open()
				}
			},
			change() {},
			cancel(type) {
				if (type === 'tip') {
					this.show = false
					return
				}
				this.$refs[type].close()
			},
		}
	}
</script>
<style>
	page{
		 background-color: #F8CF00;
	}
	.genxin_content {
		width: 636upx;
		height: 436upx;
		background-image: url(../../static/image/components/gengxin.png);
		background-size: 100% 100%;
		margin: 0 auto;
		overflow: hidden;
		/* border-radius: 8upx; */
	}

	.genxin_title {
		width: 100%;
		overflow: hidden;
		margin-top: 240upx;
		font-size: 32upx;
		text-align: center;
		padding-bottom: 12upx;
	}

	.genxin_neirong {
		width: 78%;
		margin: 0 auto;
		overflow: hidden;
		color: #F8CF00;
		font-size: 22upx;
		margin-top: 240upx;
	}

	.lijitiyan {
		width: 50%;
		margin: 0 auto;
		text-align: center;
		line-height: 62upx;
		color: #FFFFFF;
		background-size: 100% 100%;
		background-color: #F8CF00;
		border-radius: 44upx;
		font-size: 24upx;
		margin-top: 30rpx;
	}

	.guanbi {
		width: 434upx;
		margin: 0 auto;
		height: 80upx;
		margin-top: 30upx;
		position: absolute;
		margin-left: 30upx;
		text-align: right;
	}
</style>
